<?php

class User_Form {
  
	private $user;
	private $erreurs;
  
	public function __construct($user) {
		$this->user = $user;
		$this->erreurs = array(
				 'mail'=>'',
				 'name'=>'',
				 'passwd'=>'',
				 );
		$this->erreurcss = array(
				"mail" =>"",
				"name" => "",
				"mdp" =>"",
				"mdp2"=>""
				  );
		$this->messageerreur = "";
	}
  
  
  
	public function makeInscriptForm($actionUrl) {
  
		$id_user = $this->user->getIdUser();
		$mail = $this->user->getMail();
	  	$passwd = $this->user->getPassword();
		$name = $this->user->getName();

	  	$text = <<<EOT
  
  		<div id="inscriptionForm" class="lightboxform">
  			<form action="{$actionUrl}" method="post">
			<h2>Inscription</h2>
  			<div>
			 			 	<p id="rep_inscript" class="merreur">{$this->messageerreur}{$this->erreurs["mail"]}{$this->erreurs['passwd']}{$this->erreurs['name']}</p>		
  				<label for="name" class="{$this->erreurcss['name']}">Nom*</label><input type="text" id="name"  name="name" value="{$name}" />
  				<label for="mail" class="{$this->erreurcss['mail']}">Adresse E-mail*</label><input type="text" id="mail"  name="mail" value="{$mail}" />
 				<label for="passwd" class="{$this->erreurcss['mdp']}">Mot de passe*</label><input type="password" id="passwd" name="password" value="" />
				<label for="passwdconf" class="{$this->erreurcss['mdp2']}">Confirmez votre mot de passe*</label><input type="password" id="passwdconf" name="passwordconf" value="" />
  

  				<div class="submit"><input type="hidden" name="id_user" value="{$id_user}"/><input type="submit" name="go" value="Valider" /></div>
 			</div>
  			</form>
  		</div>
EOT;
	  return $text;
  
   }
  
	public function checkInscript($data) {
  		//var_dump($this->user);
	  	$flag = true;
	  	$mail = $this->user->getMail();
	  	$ligne = User_Bd::checkMail($mail);
		if (trim($mail) == ""){
			$this->erreurs['mail'] = " Entrez une adresse email valide.";
			$this->erreurcss['mail'] = "erreur";
			$flag = false;
		} else {
			if(!preg_match("/^([A-Za-z0-9]([-_.]?[A-Za-z0-9])*@[A-Za-z0-9]([-.]?[A-Za-z0-9])*[.](ac|ad|ae|af|ag|ai|al|am|an|ao|aq|ar|as|at|au|aw|az|ba|bb|bd|be|bf|bg|bh|bi|bj|bm|bn|bo|br|bs|bt|bv|bw|by|bz|ca|cc|cd|cf|cg|ch|ci|ck|cl|cm|cn|co|cr|cu|cv|cx|cy|cz|de|dj|dk|dm|do|dz|ec|ee|eg|eh|er|es|et|fi|fj|fk|fm|fo|fr|fx|ga|gb|gd|ge|gf|gg|gh|gi|gl|gm|gn|gp|gq|gr|gs|gt|gu|gw|gy|hk|hm|hn|hr|ht|hu|id|ie|il|im|in|io|iq|ir|is|it|je|jm|jo|jp|ke|kg|kh|ki|km|kn|kp|kr|kw|ky|kz|la|lb|lc|li|lk|lr|ls|lt|lu|lv|ly|ma|mc|md|mg|mh|mk|ml|mm|mn|mo|mp|mq|mr|ms|mt|mu|mv|mw|mx|my|mz|na|nc|ne|nf|ng|ni|nl|no|np|nr|nt|nu|nz|om|pa|pe|pf|pg|ph|pk|pl|pm|pn|pr|pt|pw|py|qa|re|ro|ru|rw|sa|sb|sc|sd|se|sg|sh|si|sj|sk|sl|sm|sn|so|sr|st|su|sv|sy|sz|tc|td|tf|tg|th|tj|tk|tm|tn|to|tp|tr|tt|tv|tw|tz|ua|ug|uk|um|us|uy|uz|va|vc|ve|vg|vi|vn|vu|wf|ws|ye|yt|yu|za|zm|zr|zw|com|edu|gov|int|mil|net|org|arts|firm|info|nom|rec|shop|web|arpa|biz|pro|coop|museum|aero|eu))?$/",$mail)){
				//L'email est fausse
				$this->erreurs['mail'] = " Entrez une adresse email valide.";
				$this->erreurcss['mail'] = "erreur";
				$flag = false;
			} else {
				if(is_array($ligne)){
					$this->erreurs['mail'] = " Cette adresse email est déjà utilisée.";
					$this->erreurcss['mail'] = "erreur";
					$flag = false;
				}
			}
		}
	  
		if (trim($this->user->getName()) == ""){
		  	$this->erreurs['name'] = " Entrez un nom d'utilisateur";
		  	$this->erreurcss['name'] = "erreur";
		  	$flag = false;
		}
	  
  
		if (trim($this->user->getPassword()) == "") {
			  $this->erreurs['passwd'] = " Entrez un mot de passe.";
			  $this->erreurcss['mdp'] = "erreur";
			  $flag = false;
		} else {
			if ($this->user->getPassword() != $data['passwordconf']){
				$this->erreurs['passwd'] = " Erreur dans la confirmation de mot de passe.";
				$this->erreurcss['mdp2'] = "erreur";
				$this->erreurcss['mdp'] = "erreur";
				$flag = false;
			}
		}
		
		if (!$flag){
			$this->messageerreur = "Votre saisie comporte des erreurs : ";	
		}
	
		
		return $flag;
	}
  

}
?>
